package com.ailk.aee.pcc.server;

import com.ailk.aee.pcc.dao.UserPloyStateDao;
import com.ailk.aee.pcc.utils.DateTimeUtils;
import com.asiainfo.simplecache.SimpleCache;
import com.mongodb.BasicDBObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author pengzhang
 */
@Service
public class UserPloyStateServer {

    @Autowired
    private SimpleCache userPloyStateCache;

    @Autowired
    private UserPloyStateDao userPloyStateDao;

    public void updatePloyUserState(String userId, String ployCode, String userState, String updatetime) {
//        String ployUserState = CacheUtils.getPloyUserState(userId, ployCode);
        if (StringUtils.isEmpty(updatetime)) {
            //获取时间
            updatetime = DateTimeUtils.getCurrentTime(DateTimeUtils.DATE_FORMAT_YYYYMMDDHHMISS);
        }
        userPloyStateDao.updatePloyUserState(userId, ployCode, userState, updatetime);

        //删除缓存
//        userPloyStateCache.delete(ployUserState);
    }

    public BasicDBObject queryPloyUserState(String userId, String ployCode) {
//        String ployUserState = CacheUtils.getPloyUserState(userId, ployCode);
//        Serializable serializable = userPloyStateCache.get(ployUserState);
//        BasicDBObject userState = (BasicDBObject) serializable;
//        if (serializable == null) {
            BasicDBObject userState = (BasicDBObject) userPloyStateDao.queryPloyUserState(userId, ployCode);
//            userPloyStateCache.put(ployUserState, userState);
//        }
        return userState;
    }

    public BasicDBObject queryUserState(String userId) {
        return   (BasicDBObject) userPloyStateDao.queryUserState(userId);
    }


    /**
     * 删除 策略状态
     *
     * @param userId
     * @param ployCode
     */
    public void deletePloyUserState(String userId, String ployCode) {
//        String ployUserState = CacheUtils.getPloyUserState(userId, ployCode);
        userPloyStateDao.deletePloyUserState(userId, ployCode);

//        userPloyStateCache.delete(ployUserState);

    }
}
